package org.example;

import org.example.scoredata.Addscore;
import org.example.scoredata.CountScore;
import org.example.scoredata.Score;
import org.example.scoredata.SeeAllSocres;
import org.example.topicdata.*;
import org.example.userdata.AddUser;
import org.example.userdata.TestUser;
import org.example.userdata.User;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.sql.ResultSet;

public class ServerThread extends Thread{
    Socket socket;
    DataOutputStream out = null;
    DataInputStream in = null;
    String s = null;

    boolean boo=false;//检查是否登陆成功
    ServerThread(Socket t){
        socket = t;
        try{ out = new DataOutputStream(socket.getOutputStream());
            in = new DataInputStream(socket.getInputStream());
        }
        catch (IOException e) {}
    }
    public void run(){
        ResultSet rs = null;                //接受信息处理区
        while (true){
            try {
                String str=in.readUTF();
                //处理获取数据
                if (str.startsWith("查询ID") || str.startsWith("注册ID")){
                    int ID_a = str.indexOf("ID");
                    int Name_a = str.indexOf("姓名");
                    int class_a = str.indexOf("类型");
                    int password_a = str.indexOf("密码");
                    String ID_s = str.substring(ID_a + 3,Name_a);
                    String name_s = str.substring(Name_a + 3,class_a);
                    String class_s = str.substring(class_a + 3,password_a);
                    String passwprd_s = str.substring(password_a + 3);
                    Boolean isStu = null;
                    if (class_s.equals("学生")){
                        isStu = true;
                    } else if (class_s.equals("老师")) {
                        isStu =false;
                    }else {}
                    User user = new User(ID_s,name_s,isStu,passwprd_s);
                    if (str.startsWith("查询ID")){//查询用户
                        TestUser testUser =new TestUser();
                        String ans = testUser.testUser(user);
                        out.writeUTF("查询信息  "+ans);
                    } else if (str.startsWith("注册ID")){  //注册用户
                        AddUser addUser =new AddUser();
                        int isOk = addUser.insertUser(user);
                        if (isOk != 0){
                            out.writeUTF("注册信息"+"注册成功!");
                        }
                        else {
                            out.writeUTF("注册信息"+"注册失败，ID不能重复");
                        }
                    }
                }else if (str.startsWith("添加题目")) {
                    DealTopicSteam dealTopicSteam = new DealTopicSteam();
                    Topic topic = dealTopicSteam.deal(str);
                    AddTopic addTopic =new AddTopic();
                    addTopic.insertTopic(topic);
                }else if (str.startsWith("搜索题目ID")) {
                    DealTopicSteam dealTopicSteam = new DealTopicSteam();
                    Topic topic = dealTopicSteam.deal(str);
                    SearchTopic searchTopic = new SearchTopic();
                    String search_result = searchTopic.search(topic);
                    out.writeUTF(search_result);
                }else if (str.startsWith("显示所有题目")) {
                    CountTopic countTopic = new CountTopic();
                    out.writeUTF("题目的总数为："+countTopic.testUser());
                    String showOUt;
                    Topic result;
                    System.out.println(Server.topicarrayList.size());
                    for (int i = 0; i < Server.topicarrayList.size(); i++) {
                        result = Server.topicarrayList.get(i);

                        if (i == Server.topicarrayList.size()-1){
                            showOUt = "最后显示第"+result.getID()+"题,题目内容："+result.getTopicName()+
                                    "\n选项A:"+result.getSelect_A()+"\t选项B:"+result.getSelect_B()+
                                    "\n选项C:"+result.getSelect_C()+"\t选项D:"+result.getSelect_D()+
                                    "\n答案:"+result.getAns();
                        }else {
                            showOUt = "显示第"+result.getID()+"题,题目内容："+result.getTopicName()+
                                    "\n选项A:"+result.getSelect_A()+"\t选项B:"+result.getSelect_B()+
                                    "\n选项C:"+result.getSelect_C()+"\t选项D:"+result.getSelect_D()+
                                    "\n答案:"+result.getAns();
                        }
                        out.writeUTF(showOUt);
                    }
                }else if (str.equals("开始考试发题")) {
                    String oneTopic;
                    GetTopics getTopics = new GetTopics();
                    int head = getTopics.createHeadTopics();
                    out.writeUTF("试卷第" + head);  //通知传送的题目为。。。
                    for (int i = head; i < head + 5; i++) {
                        Topic topic = Server.topicarrayList.get(i);
                        oneTopic = "第"+(i-head+1)+"题,题目内容："+topic.getTopicName()+
                                "\n\nA:"+topic.getSelect_A()+"\t\tB:"+topic.getSelect_B()+
                                "\n\nC:"+topic.getSelect_C()+"\t\tD:"+topic.getSelect_D();
                        out.writeUTF(oneTopic);
                    }
                }else if (str.startsWith("学生ID")){
                    System.out.println(str);
                    int ans_i = str.indexOf("学生答案：");
                    String s_ID = str.substring(4,ans_i);
                    String Y_str = str.substring(4);

                    int test_paper = Integer.parseInt(str.substring(ans_i+10));
                    CountScore countScore = new CountScore();

                    Addscore addscore = new Addscore();
                    int score_fen = countScore.count(test_paper,Y_str);
                    Score score = new Score();
                    score.setS_ID(s_ID);
                    score.setNum();
                    score.setScore(score_fen);

                    addscore.addData(score);
                }else if (str.startsWith("查询考生历史成绩")){
                    String ID = str.substring(8);
                    Score score = new Score();
                    score.search(ID);
                    for (int i = 0; i < score.h_score.size(); i++) {
                        out.writeUTF("成绩" +score.h_score.get(i));
                    }
                }else if (str.startsWith("删除题目")){
                    DeleteTopic deleteTopic = new DeleteTopic();
                    Topic topic = new Topic();
                    topic.setID(Integer.parseInt(str.substring(4)));
                    int imf = deleteTopic.deleteTopic(topic);

                    String result;
                    if (imf == 1){
                        result = "删除信息删除成功";
                    }else result = "删除信息删除失败，或查无此题";

                    out.writeUTF(result);
                }else if (str.startsWith("##")){
                    System.out.println(str);
                    ChangeTopics changeTopics = new ChangeTopics();
                    Topic topic = changeTopics.dealData(str);
                    System.out.println(topic.getTopicName()+"timu");

                    int imf = changeTopics.change(topic);
                    String result;
                    if (imf == 1){
                        result = "修改信息修改成功";
                    }else result = "修改信息修改失败，或查无此题";

                    out.writeUTF(result);
                }else if (str.startsWith("查看学生成绩")){
                    SeeAllSocres seeAllSocres = new SeeAllSocres();
                    int num =seeAllSocres.see();
                    Score result;
                    for (int i = 0; i < num; i++) {
                        result = Server.SocrearrayList.get(i);
                        String st = "学生成绩##" + result.getS_ID() + "##" + result.getScore();
                        out.writeUTF(st);
                    }
                    Server.SocrearrayList.clear();
                }else if (str.startsWith("搜索学生ID")){
                    String ID = str.substring(6);
                    SeeAllSocres seeAllSocres_1 = new SeeAllSocres();
                    int num = seeAllSocres_1.see();
                    Score result;
                    System.out.println(ID);
                    for (int i = 0; i < num; i++) {
                        if (ID.equals(Server.SocrearrayList.get(i).getS_ID().trim())){
                            result = Server.SocrearrayList.get(i);
                            String st = "学生成绩##" + result.getS_ID() + "##" + result.getScore();
                            out.writeUTF(st);
                        }
                    }
                    Server.SocrearrayList.clear();
                }
            }catch (IOException e){
                return;
            }
        }
    }

}
